package com.jianjian.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jianjian.common.result.Result;
import com.jianjian.model.system.SysRole;
import com.jianjian.model.vo.AssginRoleVo;
import com.jianjian.model.vo.SysRoleQueryVo;
import com.jianjian.system.handler.GuiGuException;
import com.jianjian.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Zsir
 * @create 2022-10-26 16:03
 */
@Slf4j
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    //用户重新分配角色
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        roleService.doAssign(assginRoleVo);
        return Result.ok();
    }

    //根据用户获取角色数据
    @ApiOperation("获取用户角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable("userId") Long userId){
        Map<String,Object> map =  roleService.getRolesByUserId(userId);
        return Result.ok(map);
    }



    //3.条件分页查询
    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result findPageQueryRole(@ApiParam(name = "page", value = "当前页码", required = true)
                                        @PathVariable Long page,

                                    @ApiParam(name = "limit", value = "每页记录数", required = true)
                                        @PathVariable Long limit,

                                    @ApiParam(name = "roleQueryVo", value = "查询对象", required = false)
                                                SysRoleQueryVo roleQueryVo){
        //插件page对象
        Page<SysRole> pageParam = new Page<>(page,limit);
        IPage<SysRole> pageModel =  roleService.selectPage(pageParam,roleQueryVo);
       //返回
        return Result.ok(pageModel);
    }

    /**
     *  4.添加角色
     *  RequestBody：不能使用Get提交方式
     *  传递json数据格式，把json数据封装到对象中
     */
    @ApiOperation(value = "添加角色")
    @PostMapping("save")
    public  Result save(@ApiParam(name = "sysRole", value = "角色对象", required = true)
                         @RequestBody(required = true) SysRole sysRole){

       if (StringUtils.isEmpty(sysRole.getRoleName())){
           return Result.fail("请输入角色名");
       }
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name",sysRole.getRoleName());
        if (roleService.getOne(wrapper) != null)
        {
            return Result.fail("角色名已经存在");
        };

        Boolean is_success =  roleService.save(sysRole);
        return is_success ? Result.ok("添加成功") : Result.fail();

    }

    /**
     *  6.修改角色
     */
    @ApiOperation(value = "修改角色")
    @PostMapping("update")
    public  Result update(@ApiParam(name = "sysRole", value = "修改角色", required = true)
                         @RequestBody(required = true) SysRole sysRole){

       if (StringUtils.isEmpty(sysRole.getId())){
           return Result.fail("请传入id");
       }
        Boolean is_success =  roleService.updateById(sysRole);
        return is_success ? Result.ok("修改成功") : Result.fail();

    }

    //5.根据id查询
    @ApiOperation("根据id查询角色")
    @GetMapping("findRoleById/{id}")
    public Result findRoleById(@ApiParam(name = "id", value = "角色id", required = true)
                                @PathVariable("id") Long id){
        SysRole sysRole = roleService.getById(id);
        return Result.ok(sysRole);
    }

    //7.批量删除  json格式的数组 就是java中的集合List
    @ApiOperation(value = "批量删除接口")
    @DeleteMapping("removeBatchIds")
    public Result removeBatchIds(@ApiParam(name = "ids", value = "多角色id", required = true)
                                @RequestBody(required = true) List<Long> ids){
        return  roleService.removeByIds(ids) ? Result.ok() : Result.fail();

    }


    //1.查询所有的角色
    @ApiOperation(value = "查询所有的角色接口")
    @GetMapping("findAll")
    public Result findAll(){
        try {
            int i =1 /0 ;
        }catch (Exception e){
            //手动抛出异常
            throw new GuiGuException(20001,"执行自定义异常");
        }


        //调用service
        return Result.ok(roleService.list());
    }

    //2.逻辑删除
    @ApiOperation(value = "逻辑删除接口")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable("id") long id ){
    return  roleService.removeById(id) ? Result.ok() : Result.fail();
    }

}
